//
//  ZYHomeVc.swift
//  SwiftDemo1
//
//  Created by wzp on 2025/5/31.
//

import Foundation
import UIKit

class ZYHomeVc: ZYBaseViewController {
    private var _animationViewLoaded = false
    private lazy var poperAnimator = {
        let poperAnimator = ZYPoperAnimator {[weak self] isPresented in
            if let button = self?.navigationItem.titleView as? UIButton {
                button.isSelected = isPresented
            }
        }
        let width: CGFloat = 200
        let height: CGFloat = 270
        let y:CGFloat = topBarHeight() - 5
        let x:CGFloat = (UIScreen.main.bounds.size.width - width) / 2
        poperAnimator.presentedViewFrame = CGRect(x:x , y: y, width: width, height: height)
        return poperAnimator
    }()
    
    lazy private var animationView: ZYVisitorAnimationView = {
        let animationView = ZYVisitorAnimationView()
        animationView.updateUI("visitordiscover_feed_image_house", animationImageName: "visitordiscover_feed_image_smallicon")
        _animationViewLoaded = true
        return animationView
    }()
    
    private lazy var viewModels = [ZYStatusViewModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadNewStatus()
    }
    
    override func updateVisitorView() {
        visitorView.updateDescText("关注一些人，回这里看看有什么惊喜")
        visitorView.addDisplayView(animationView)
        animationView.startAnimation()
    }
    
    deinit {
        if _animationViewLoaded {
            animationView.stopAnimation()
        }
    }
    
    override func setupNavigationBar() {
        super.setupNavigationBar()
        if !isLogin {
            return
        }
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendattention", target: self, action: #selector(clickFriendItem))
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(clickScanItem))
        
        //写titleView
        let button: ZYImageRightButton = ZYImageRightButton("首页", target: self, action: #selector(clickHomeTitleItem(_:)))
        button.setImage(UIImage(named: "navigationbar_arrow_down"), for: .normal)
        button.setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        button.sizeToFit()
        button.frame.size.height = 35
        navigationItem.titleView = button
    }
}

// MARK: -设置UI
extension ZYHomeVc {
    
}

// MARK: -Event
extension ZYHomeVc {
    @objc func clickFriendItem() {
        
    }
    
    @objc func clickScanItem() {
        
    }
    
    @objc func clickHomeTitleItem(_ sender: UIButton) {
        if !sender.isSelected {
            let popVc = ZYPopViewController()
            popVc.modalPresentationStyle = .custom
            popVc.transitioningDelegate = poperAnimator
            present(popVc, animated: true)
        }
    }
}
// MARK: -数据逻辑
extension ZYHomeVc {
    @objc private func loadNewStatus() {
        loadStatus(false)
    }
    
    @objc private func loadMoreStatus() {
        loadStatus(true)
    }
    
    private func loadStatus(_ isMore: Bool) {
        var since_id = 0
        var max_id = 0
        
        if !isMore {
            since_id = viewModels.first?.status?.mid ?? 0
        } else {
            max_id = viewModels.last?.status?.mid ?? 0
            max_id = max_id == 0 ? 0 : (max_id - 1)
        }
        
        ZYRequestCore.shareCore.loadHomeStatuses(since_id: since_id, max_id: max_id) { result in
            switch result {
            case .success(let json):
                Logger.info(json)
            case .failure(let error):
                Logger.info(error.localDesc)
            }
        }
    }
}
